package CarManagement;

import javax.swing.*;
import java.awt.event.*;

public class updataCar extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JTextField textField8;
    private JTextField textField9;
    private JTextField textField10;
    private JTextField textField11;

    public updataCar(Car car) {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        if(car instanceof BigCar){
            textField1.setText(car.getLicense());
            textField2.setText(car.getCompany());
            textField3.setText(car.getBuyTime());
            textField4.setText(car.getMile()+"");
            textField5.setText(car.getOilConsumption()+"");
            textField6.setText(car.getProtectMoney()+"");
            textField7.setText(car.getRoadMoney()+"");
            textField8.setText(car.getSum()+"");
            textField9.setText(((BigCar)car).getPassengerNum()+"");
        }
        if(car instanceof SmallCar){
            textField1.setText(car.getLicense());
            textField2.setText(car.getCompany());
            textField3.setText(car.getBuyTime());
            textField4.setText(car.getMile()+"");
            textField5.setText(car.getOilConsumption()+"");
            textField6.setText(car.getProtectMoney()+"");
            textField7.setText(car.getRoadMoney()+"");
            textField8.setText(car.getSum()+"");
            textField10.setText(((SmallCar)car).getBoxNum()+"");
        }
        if(car instanceof Track){
            textField1.setText(car.getLicense());
            textField2.setText(car.getCompany());
            textField3.setText(car.getBuyTime());
            textField4.setText(car.getMile()+"");
            textField5.setText(car.getOilConsumption()+"");
            textField6.setText(car.getProtectMoney()+"");
            textField7.setText(car.getRoadMoney()+"");
            textField8.setText(car.getSum()+"");
            textField11.setText(((Track)car).getWeight()+"");
        }

        buttonOK.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String license,company,buyTime;
                int mile,protectMoney,roadMoney;
                float oilConsumption,sum ;
                String id=car.getId();
                String t=car.getType();
                if(t.equals("大客车")){
                    license = textField1.getText();
                    company = textField2.getText();
                     buyTime= textField3.getText();
                    mile = Integer.parseInt(textField4.getText());
                    oilConsumption =Float.parseFloat(textField5.getText()) ;
                    protectMoney=Integer.parseInt(textField6.getText());
                    roadMoney = Integer.parseInt(textField7.getText());
                    sum=Float.parseFloat(textField8.getText());
                    int  passengerNum = Integer.parseInt(textField9.getText());
                    BigCar bigCar = new BigCar(id, license, company, buyTime, t, mile, oilConsumption, protectMoney, roadMoney, sum, passengerNum);
                    Service.modify(bigCar);
                }
                if(t.equals("小轿车")){
                    license = textField1.getText();
                    company = textField2.getText();
                    buyTime= textField3.getText();
                    mile = Integer.parseInt(textField4.getText());
                    oilConsumption =Float.parseFloat(textField5.getText()) ;
                    protectMoney=Integer.parseInt(textField6.getText());
                    roadMoney = Integer.parseInt(textField7.getText());
                    sum=Float.parseFloat(textField8.getText());
                    int  boxNum = Integer.parseInt(textField10.getText());
                    SmallCar smallCar = new SmallCar(id, license, company, buyTime, t, mile, oilConsumption, protectMoney, roadMoney, sum, boxNum);
                    Service.modify(smallCar);
                }
                if(t.equals("卡车")){
                    license = textField1.getText();
                    company = textField2.getText();
                    buyTime= textField3.getText();
                    mile = Integer.parseInt(textField4.getText());
                    oilConsumption =Float.parseFloat(textField5.getText()) ;
                    protectMoney=Integer.parseInt(textField6.getText());
                    roadMoney = Integer.parseInt(textField7.getText());
                    sum=Float.parseFloat(textField8.getText());
                    int  weight = Integer.parseInt(textField11.getText());
                    Track track = new Track(id, license, company, buyTime, t, mile, oilConsumption, protectMoney, roadMoney, sum, weight);
                    Service.modify(track);
                }
                dispose();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 单击十字时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // 在此处添加代码
        dispose();
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }


}
